<?php
namespace submodule\base;


use base\YiiMobo;
use base\BaseView;

/**
 * Site controller
 */
class BaseSubmoduleView extends \backend\base\BaseBackendView
{

   	public function indexFieldsValues($models, $setting, $rows, $function = NULL){
		$field = $setting['field'];
		$fieldid = $setting['fieldid'];
		$view = "@submodule/models_views/{$models['models']['tablename']}/$field/index";
		if (method_exists($function, $field.'Index')){
			$viewfunction = $field.'Index';
			$val = $function->$viewfunction($fieldid, $field, $rows);
		}else if(file_exists($this->findViewFile($view))) {
			$val = $this->render($view, array(
				'models'=>$models,
				'setting'=>$setting,
				'data'=>$rows,
			), true);
			
		}else{
			$val = $rows[$field];
		}
		
		//$values = $this->models['setting']['index']['values'][$fieldid];
		//$val = $values ? preg_replace('/{(.*?)}/e',"$1", $values) : $rows[$field];
		return $val;
	}

	public function createOrderbyUrl($val, $orderby, $action = 'models-form/index'){
		$field = ($val['primarytable'] ? 'a' : 'b').'.'.$val['field'].'';
		$orderby[0] = str_replace(['a.','b.'], '', $orderby[0]);
		unset($_REQUEST['_csrf']);
        return $this->createUrl(array_merge($_REQUEST, array($action, 'orderby'=> $orderby[0] == $val['field'] && $orderby[1]=='desc' ? $field.' asc' : $field.' desc')));


	}
}
